<template>
  <div>
    <span>停车场管理员优惠券管理</span>
    <br/>
    <!-- 搜索筛选 -->
    <el-button type="text" @click="dialogFormVisible = true">添加优惠券</el-button>


    <el-form  :inline="true"  :model="formInline" class="user-search">
      <el-form-item label="优惠券的名字">
        <el-input size="small" v-model="formInline.cName" placeholder="优惠券的名字"></el-input>
      </el-form-item>
      <el-form-item label="优惠券状态">
        <el-select v-model="formInline.cStatus" placeholder="请选择">
          <el-option label="请选择" value=""></el-option>
          <el-option label="上架" value="0"></el-option>
          <el-option label="下架" value="2"></el-option>
          <el-option label="过期" value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="优惠券创建时间" :label-width="formLabelWidth">
        <div class="block">
          <el-date-picker
            v-model="formInline.cStartTime"
            type="date"
            placeholder="选择日期时间"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss"
            align="right"
            :picker-options="pickerOptions">
          </el-date-picker>
        </div>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="cName" label="优惠券名称">
      </el-table-column>
      <el-table-column sortable prop="cTotal" label="优惠券数量" :formatter="formatCTotal">
      </el-table-column>
      <el-table-column sortable prop="cDiscount" label="优惠金额">
      </el-table-column>

      <el-table-column sortable prop="cMin" label="最低消费">
      </el-table-column>
      <el-table-column sortable prop="cLimit" label="领取限制" :formatter="formatCLimit">
      </el-table-column>
      <el-table-column sortable prop="cStartTime" label="开始时间" :formatter="dateFormat">
      </el-table-column>
      <el-table-column sortable prop="cEndTime" label="结束时间" :formatter="dateFormat">
      </el-table-column>
      <el-table-column sortable prop="cStatus" label="状态" :formatter="formatStatus">
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          &nbsp;&nbsp;&nbsp;<el-button size="mini" type="primary" @click="editPKCoupon(scope.$index, scope.row)">编辑</el-button>
          <el-button size="mini" type="success" @click="EnablePKCoupon(scope.$index, scope.row)">上架</el-button>
          <el-button size="mini" type="warning" @click="DisablePKCoupon(scope.$index, scope.row)">下架</el-button>
          <el-button size="mini" type="danger"  @click="DeletePKCoupon(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 编辑界面 -->
    <el-dialog :title="title" :visible.sync="editFormVisible" @click="closeDialog">
      <el-form label-width="120px" :model="editForm" :rules="rules" ref="editForm">
        <el-form-item label="优惠券名称" :label-width="formLabelWidth" :required="true">
          <el-input v-model="editForm.cName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="优惠券数量" :label-width="formLabelWidth" :required="true">
          <el-input type="number" min="0" v-model="editForm.cTotal" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="优惠金额" :label-width="formLabelWidth" :required="true">
          <el-input type="number" min="0" v-model="editForm.cDiscount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="最少消费金额" :label-width="formLabelWidth" :required="true">
          <el-input type="number" min="0" v-model="editForm.cMin" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="优惠券开始时间" :label-width="formLabelWidth" :required="true">
          <div class="block">
            <el-date-picker
              v-model="editForm.cStartTime"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              align="right"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="优惠券结束时间" :label-width="formLabelWidth" :required="true">
          <div class="block">
            <el-date-picker
              v-model="editForm.cEndTime"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              align="right"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="领取限制数量" :label-width="formLabelWidth">
          <el-radio v-model="editForm.cLimit" :label="0">无限制</el-radio>
          <el-radio v-model="editForm.cLimit" :label="1" >限领一张</el-radio>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio v-model="editForm.cStatus" :label="0" >可用</el-radio>
          <el-radio v-model="editForm.cStatus" :label="2" >下架</el-radio>
        </el-form-item>
        <el-form-item label="优惠券简介" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="editForm.cDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="closeDialog">取消</el-button>
        <el-button size="small" type="primary" :loading="loading" class="title" @click="submitEditPKCoupon">保存</el-button>
      </div>
    </el-dialog>
    <!--添加优惠券界面-->
    <el-dialog title="添加优惠券" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="优惠券名称" :label-width="formLabelWidth" :required="true">
          <el-input v-model="form.cName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="优惠券数量" :label-width="formLabelWidth" :required="true">
          <el-input type="number" min="0" v-model="form.cTotal" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="优惠金额" :label-width="formLabelWidth" :required="true">
          <el-input type="number" min="0" v-model="form.cDiscount" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="最少消费金额" :label-width="formLabelWidth" :required="true">
          <el-input type="number" min="0" v-model="form.cMin" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="优惠券开始时间" :label-width="formLabelWidth" :required="true">
          <div class="block">
            <el-date-picker
              v-model="form.cStartTime"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              align="right"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="优惠券结束时间" :label-width="formLabelWidth" :required="true">
          <div class="block">
            <el-date-picker
              v-model="form.cEndTime"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd HH:mm:ss"
              format="yyyy-MM-dd HH:mm:ss"
              align="right"
              :picker-options="pickerOptions">
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="领取限制数量" :label-width="formLabelWidth">
          <el-radio v-model="form.cLimit" :label="0">无限制</el-radio>
          <el-radio v-model="form.cLimit" :label="1" >限领一张</el-radio>
        </el-form-item>
        <el-form-item label="状态" :label-width="formLabelWidth">
          <el-radio v-model="form.cStatus" :label="0" >可用</el-radio>
          <el-radio v-model="form.cStatus" :label="2" >下架</el-radio>
        </el-form-item>
        <el-form-item label="优惠券简介" :label-width="formLabelWidth">
          <el-input type="textarea" v-model="form.cDesc" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="addPKCoupon">确定添加</el-button>
      </div>
    </el-dialog>


  </div>
</template>

<script>
import Pagination from '../../components/Pagination';
import moment from 'moment';
// import {addPKCoupon,getPKCoupons,submitEditPKCoupon} from '../../http/api'
import {addPKCoupon,getPKCoupons,submitEditPKCoupon} from '../../api/vipMG'
export default {
  data() {
    return {
      nshow: true, //switch开启
      fshow: false, //switch关闭
      loading: false, //是显示加载
      editFormVisible: false, //控制编辑页面显示与隐藏
      dialogFormVisible: false,
      formLabelWidth: '120px',
      title: '添加',
      form: {
        cId:'',
        cName:'',
        cDesc:'',
        cTotal:'',
        cDiscount:'',
        cMin:'',
        cLimit:0,
        cStatus:0,
        cStartTime:'',
        cEndTime:'',
      },
      editForm: {
        cId:'',
        cName:'',
        cDesc:'',
        cTotal:'',
        cDiscount:'',
        cMin:'',
        cLimit:0,
        cStatus:0,
        cStartTime:'',
        cEndTime:'',

      },
      // rules表单验证
      rules: {
        deptName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        deptNo: [{ required: true, message: '请输入部门代码', trigger: 'blur' }]
      },
      formInline: {
        page: 1,
        limit: 5,
        // varLable: '',
        // varName: '',
        cName: '',
        cStatus: '',
        // cStartTime: '',
      },
      // 删除部门
      seletedata: {
        ids: '',
        token: localStorage.getItem('logintoken')
      },
      userparm: [], //搜索权限
      listData: [], //用户数据
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 4,
        total: 10
      },
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick(picker) {
            picker.$emit('pick', new Date());
          }
        }, {
          text: '昨天',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24);
            picker.$emit('pick', date);
          }
        }, {
          text: '一周前',
          onClick(picker) {
            const date = new Date();
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', date);
          }
        }]
      },

    }
  },
  // 注册组件
  components: {
    Pagination
  },

  /**
   * 创建完毕
   */
  created() {
    this.getPKCoupons(this.formInline);
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    formatStatus(row, column, cellValue, index){
      return cellValue==0?'可用':cellValue==1?'过期':cellValue==2?'下架':'未知';
    },
    formatCTotal(row, column, cellValue, index){
      return cellValue==0?'无限量':cellValue+'';
    },
    formatCLimit(row, column, cellValue, index){
      return cellValue==0?'不限制':cellValue==1?'限领一张':'未知';
    },
    dateFormat(row, column, cellValue, index){
      return moment(cellValue).format("YYYY-MM-DD HH:mm:ss")
    },

    addPKCoupon(){
      console.log("add form优惠券1",this.form);
      addPKCoupon(this.form).then(response=>{
        console.log("add form优惠券2",this.form);
        // let result = response.data;
        let result = response;
        if (result.code==0){
          this.dialogFormVisible = false;
          this.$message({
            type: 'success',
            message: result.msg,
          });
          this.getPKCoupons();
        }else if(result.code==1){
          // this.dialogFormVisible = false;
          this.$message({
            type: 'error',
            message: result.msg,
          });
          // this.getVipTypes();
        }
      });
    },
    getPKCoupons(coupon) {
      this.loading = true;
      console.log("coupon:=======》》》》",coupon);
      getPKCoupons(coupon)
        .then(response => {
          // let result = response.data;
          let result = response;
          this.loading = false;
          if (result.code == 0) {
            this.listData = result.data.list;
            console.log("result.data.list:",result.data.list);
            // 分页赋值
            // this.pageparm.currentPage = this.formInline.page;
            // this.pageparm.pageSize = this.formInline.limit;
            // this.pageparm.total = result.data.total;
            this.pageparm.currentPage = this.formInline.page;
            this.pageparm.pageSize = this.formInline.limit;
            this.pageparm.total = result.data.total;
          } else {
            this.$message({
              type: 'info',
              message: result.msg,
            })
          }
        })
        .catch(err => {
          this.loading = false;
          this.$message.error('用户vip列表加载失败，请稍后再试！')
        })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage;
      this.formInline.limit = parm.pageSize;
      this.getPKCoupons(this.formInline);
    },
    // 搜索事件
    search() {
      this.getPKCoupons(this.formInline);
    },
    editPKCoupon: function(index, row) {
      this.editFormVisible = true;
      this.editForm=row;
      // console.log(" this.editForm----》》》",this.editForm);
    },
    submitEditPKCoupon() {
      console.log("this.editForm:",this.editForm);

        this.editForm.page=1;
        this.editForm.limit=5;
      submitEditPKCoupon(this.editForm).then(response=>{
        // let result = response.data;

        let result = response;
        if (result.code==0){
          this.editFormVisible = false;
          this.getPKCoupons(this.formInline);
          this.$message({
            type: 'success',
            message: result.msg,
          });
        } else if (result.code==1) {
          this.editFormVisible = true;
          this.getPKCoupons(this.formInline);
          this.$message({
            type: 'error',
            message: result.msg,
          });
        }
      }).catch(err => {
        this.editFormVisible = false;
        this.loading = false;
        this.getPKCoupons(this.formInline);
        this.$message.error('修改失败，请稍后再试！');
      });
    },
    // 上架
    EnablePKCoupon(index, row) {
      let coupon={};
      coupon.cId=row.cId;
      coupon.cStatus=0;

      submitEditPKCoupon(coupon).then(response=>{
        // let result = response.data;
        let result = response;
        if (result.code==0){
          this.getPKCoupons(this.formInline);
          this.$message({
            type: 'success',
            message: result.msg,
          });
        } else if (result.code==1) {
          this.getPKCoupons(this.formInline);
          this.$message({
            type: 'error',
            message: result.msg,
          });
        }
      }).catch(err => {
        this.editFormVisible = false;
        this.loading = false;
        this.getPKCoupons(this.formInline);
        this.$message.error('启用失败，请稍后再试！');
      });
    },
    // DisablePKCoupon  下架
    DisablePKCoupon(index, row) {
      let coupon={};
      coupon.cId=row.cId;
      coupon.cStatus=2;

      submitEditPKCoupon(coupon).then(response=>{
        // let result = response.data;
        let result = response;
        if (result.code==0){
          this.getPKCoupons(this.formInline);
          this.$message({
            type: 'success',
            message: result.msg,
          });
        } else if (result.code==1) {
          this.getPKCoupons(this.formInline);
          this.$message({
            type: 'error',
            message: result.msg,
          });
        }
      }).catch(err => {
        this.editFormVisible = false;
        this.loading = false;
        this.getPKCoupons(this.formInline);
        this.$message.error('启用失败，请稍后再试！');
      });
    },
    DeletePKCoupon(index, row) {
      let coupon={};
      coupon.cId=row.cId;
      coupon.isDeleted=1;
      // console.log("coupon:==>",coupon);
      submitEditPKCoupon(coupon).then(response=>{
        // let result = response.data;
        let result = response;
        if (result.code==0){
          this.getPKCoupons(this.formInline);
          this.$message({
            type: 'success',
            message: result.msg,
          });
        } else if (result.code==1) {
          tthis.getPKCoupons(this.formInline);
          this.$message({
            type: 'error',
            message: result.msg,
          });
        }
      }).catch(err => {
        this.editFormVisible = false;
        this.loading = false;
        this.getPKCoupons(this.formInline);
        this.$message.error('启用失败，请稍后再试！');
      });
    },

    // 关闭编辑、增加弹出框
    closeDialog() {
      this.editFormVisible = false;
    }
  }
}
</script>

<style scoped>
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
</style>
